// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    selectedImg: [],
    loading_show: true,
    btn: [
        {
            url: "../like/like",
            icon: "../../images/like.png",
            btn_title: "猜你喜欢"
        },
        {
            url: "../activity/activity",
            icon: "../../images/youhui.png",
            btn_title: "优惠活动"
        },
        {
            url: "../collocation/collocation",
            icon: "../../images/sheji.png",
            btn_title: "推荐搭配"
        }
    ],
    default_img:"../../images/bannerDefaultImg.jpg",
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const that = this;
    that.init();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init: function () {
    wx.getSetting({
      success: (res) => {
        if (typeof res.authSetting['scope.userInfo'] === 'undefined') {
          this.setData({
            flag: false,
          })
        } else {
          this.wxLogin();
        }
        /*
          * res.authSetting = {
          *   "scope.userInfo": true,
          *   "scope.userLocation": true
          * }
          */
      }
    })


      var that = this;
      setTimeout(function () {
          that.setData({
              loading_show: false
          })
      }, 1000)
      wx.request({
          url: app.config() + 'index/getChoice',
          success: function (res) {
              if (res.data.data != "") {
                  var banner = res.data.data.banner.data;
                  for (var k = 0; k < banner.length;k++){
                      var imageUrl = banner[k]['imageUrl'];
                      banner[k]['imageUrl'] = banner[k]['imageUrl']+"?v="+Date.parse(new Date())
                  }
                  var choiceData = res.data.data.goods;
                  var data = new Array();
                  var shirtArr = {
                      title: "衬衣",
                      data: []
                  };//衬衣
                  var suitArr = {
                      title: "西装",
                      data: []
                  };//西装
                  var overcoatArr = {
                      title: "大衣",
                      data: []
                  };//大衣
                  var decorateArr = {
                      title: "配饰",
                      data: []
                  };//配饰
                  var shirt = new Array();//衬衣
                  var suit = new Array();//西装
                  var overcoat = new Array();//大衣
                  var decorate = new Array();//配饰
                  for (var i = 0; i < choiceData.length; i++) {
                      var selected_url = choiceData[i].selected_url;
                      choiceData[i]['selected_url'] = selected_url+"?v="+Date.parse(new Date());
                      var goods_type = choiceData[i].type;
                      if (goods_type == "衬衣") {
                          shirtArr.data.push(choiceData[i]);
                      } else if (goods_type == "西装") {
                          suitArr.data.push(choiceData[i]);
                      } else if (goods_type == "大衣") {
                          overcoatArr.data.push(choiceData[i]);
                      } else if (goods_type == "配饰") {
                          decorateArr.data.push(choiceData[i]);
                      }
                  }
                  if (shirtArr.data != "") {
                      data.push(shirtArr)
                  }
                  if (suitArr.data != "") {
                      data.push(suitArr)
                  }
                  if (overcoatArr.data != "") {
                      data.push(overcoatArr)
                  }
                  if (decorateArr.data != "") {
                      data.push(decorateArr)
                  }
                  that.setData({
                      imgUrls: banner,
                      data: data
                  })
              }

          }
      })
  },//根据open_id自动登陆
  auto_login: function (open_id) {
      var params = {};
      params['open_id'] = open_id;
      wx.request({
          url: app.config() + "member/xiaoLoginByWx",
          data: params,
          header: {
              'content-type': 'application/json'
          },
          success: function (res) {
              var data = res.data;
              if (data.status == 0) {
                  // 登陆成功
                  data.data['open_id'] = open_id;
                  wx.setStorage({
                      key: 'memberInfo',
                      data: data.data
                  })
              } else {
                  //登陆失败
                  var params = {};
                  params['open_id'] = open_id;
                  wx.setStorage({
                      key: 'memberInfo',
                      data: params,
                  })
              }
          }
      })
  },//提交用户信息
  submit_user_info: function (args) {
      wx.request({
          url: app.config() + "index/xiaoWxInfo",
          data: args,
          header: {
              'content-type': 'application/json'
          },
          success: function (res) {

          }
      })
  },
  myevent(e) {
    this.setData({
      flag: e.detail.flag,
    })
  },
  wxLogin() {
    const that = this;
    wx.login({
      success: function (res) {
        var args = {};
        args['code'] = res.code;
        args['grant_type'] = "authorization_code";
        wx.request({
          url: app.config() + 'index/getXiaoToken',
          data: args,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            if (res.data.status == 0) {
              var open_id = res.data.data.openid;
              that.auto_login(open_id);
            }
          }
        })
      }
    })
  }
})